package com.hitqz.robot.dispatch.netty.service.impl;

import com.hitqz.robot.api.business.entity.RobotEntity;
import com.hitqz.robot.api.business.entity.RobotGroupEntity;
import com.hitqz.robot.api.dispatch.protocol.dto.RobotApplyPathDto;
import com.hitqz.robot.dispatch.netty.strategy.AlgorithmStrategy;
import com.hitqz.robot.dispatch.netty.strategy.publicPathStrategy.util.PublicPathStrategyUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 调度算法--公共路径
 * @author xupkun
 * @date 2024/8/19
 */
@Service("publicPathStrategy")
@RequiredArgsConstructor
@Slf4j
public class PublicPathStrategyServiceImpl implements AlgorithmStrategy {

    public static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10,20,
            1L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(20),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.DiscardPolicy());


    @Override
    public void changePath(RobotGroupEntity robotGroup, RobotApplyPathDto robotApplyPathDto) {
        threadPool.execute(()-> {
            try {
                PublicPathStrategyUtil.travel(robotGroup, robotApplyPathDto);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }

    @Override
    public void calculate(int groupId, List<RobotEntity> robotEntityList) {
        threadPool.execute(()-> {
            for (RobotEntity robotEntity : robotEntityList) {
                PublicPathStrategyUtil.getOrSetPathList(robotEntity.getRobotSn(), null, null, null,false);
                PublicPathStrategyUtil.finishedPath(groupId, robotEntity.getRobotSn());
            }
        });
        threadPool.execute(()-> {
            PublicPathStrategyUtil.dispatchMonitor(groupId);
        });
    }


}
